/**
 * 给定N个友好、敌对关系，问是否有矛盾。
 * 跑两遍，第一遍把所有友好关系加入并查集
 * 第二遍对每一个敌对关系查询是否祖先一样，一样就是有冲突
 */
#include <bits/stdc++.h>
using namespace std;

using llt = long long;

struct uf_t{

vector<int> father;

void init(int n){father.assign(n + 1, 0);for(int i=1;i<=n;++i)father[i]=i;}
int find(int x){return x == father[x] ? x : father[x] = find(father[x]);}
void unite(int x, int y){father[find(y)] = find(x);}

}UF;

int N;
vector<tuple<int, int, int>> A;
vector<int> W;

bool proc(){	
	W.clear(); W.reserve(N + N + 1);
	W.emplace_back(0);
	for(auto [a, b, c] : A){
        W.emplace_back(a); W.emplace_back(b);
	}
	sort(W.begin(), W.end());
	W.erase(unique(W.begin(), W.end()), W.end());

	UF.init(W.size());
	for(auto [a, b, c] : A){
		if(c){
			a = lower_bound(W.begin(), W.end(), a) - W.begin();
			b = lower_bound(W.begin(), W.end(), b) - W.begin();
			UF.unite(a, b);
		}
	}

	for(auto [a, b, c] : A){
		if(0 == c){
			a = lower_bound(W.begin(), W.end(), a) - W.begin();
			b = lower_bound(W.begin(), W.end(), b) - W.begin();
			if(UF.find(a) == UF.find(b)) return false;			
		}
	}
	return true;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int nofkase = 1;
	cin >> nofkase;
	while(nofkase--){
		cin >> N;
		A.assign(N, {});
		for(auto & [a, b, c] : A) cin >> a >> b >> c;
        cout << (proc() ? "YES\n" : "NO\n");
	}
    return 0;
}